#include "data/db_struct.h"
#include "ui/model_login_log.h"

LoginLogList::LoginLogList()
{
    _model = NULL;
    _list.clear();
    _sync = false;
}
bool LoginLogList::AddNode(uint32_t account_id, const char* account_name,
        const oralib::datetime& login_time, const oralib::datetime& logout_time,
        uint32_t online_time,
        const char* ip, uint32_t port, uint32_t serverid)
{
    LoginLogNode temp_node;
    temp_node._account_id = account_id;
    temp_node._account_name = account_name;
    temp_node._login_time = login_time;
    temp_node._logout_time = logout_time;
    temp_node._ip = ip;
    temp_node._port = port;
    temp_node._serverid = serverid;
    if (logout_time.year() == 1900 &&
        logout_time.mon() == oralib::jan && logout_time.day() == 1)
        temp_node._online_time = 0;
    else
        temp_node._online_time = online_time;

    _list.push_back(temp_node);

    if (_sync && _model != NULL)
        _model->InsertNode(&(_list[_list.size() - 1]));
    return true;
}

void LoginLogList::Clean()
{
    _list.clear();
}

bool LoginLogList::OpenSync()
{
    if (!_sync && _model != NULL)
    {
        _model->Clear();
        _sync = true;
        return true;
    }
    else
        return false;
}

bool LoginLogList::CloseSync()
{
    if (_sync)
    {
        _sync = false;
        return true;
    }
    else
        return false;
}
